package com.xingxuecn.limit.web.action.sys;

import com.opensymphony.xwork2.ActionSupport;
import com.xingxuecn.limit.common.model.Role;
import com.xingxuecn.limit.common.model.vo.Page;
import com.xingxuecn.limit.common.model.vo.ResultModel;
import com.xingxuecn.limit.common.utils.ResponseUtils;
import com.xingxuecn.limit.service.RoleService;
import com.xingxuecn.limit.service.impl.RoleServiceImpl;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletResponse;
import java.sql.SQLException;
import java.util.List;

/**
 * @author Zy at 2018/9/10 0010 下午 4:05
 */
    public class Action extends ActionSupport {

        private RoleService roleService = new RoleServiceImpl();

        private Integer page;
        private Integer rows;
        private Role role = new Role();


        public String index() {
            return SUCCESS;
        }

        public String loadAll() throws SQLException {
            HttpServletResponse response = ServletActionContext.getResponse();
            ResponseUtils.writeJson(roleService.findAll(), response);
            return null;
        }

        public String search() {
            HttpServletResponse response = ServletActionContext.getResponse();
            List<Role> list = null;
            try {
                list = roleService.search(role);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            ResponseUtils.writeJson(list, response);
            return null;
        }

        public String searchPage() {
            HttpServletResponse response = ServletActionContext.getResponse();
            Page result = null;
            try {
                result = roleService.searchPage(role,page,rows);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            ResponseUtils.writeJson(result, response);
            return null;
        }


        public String add() {
            ResultModel resultModel = null;
            try {
                roleService.add(role);
            } catch (SQLException e) {
                e.printStackTrace();
                resultModel = new ResultModel(500, "新增失败");
            }
            resultModel = new ResultModel(201, "新增成功");
            HttpServletResponse response = ServletActionContext.getResponse();
            ResponseUtils.writeJson(resultModel, response);
            return null;
        }

        public String update() {
            ResultModel resultModel = null;
            try {
                roleService.update(role);
            } catch (SQLException e) {
                e.printStackTrace();
                resultModel = new ResultModel(500, "修改失败");
            }
            resultModel = new ResultModel(200, "修改成功");
            HttpServletResponse response = ServletActionContext.getResponse();
            ResponseUtils.writeJson(resultModel, response);
            return null;
        }


        public String delete() {
            ResultModel resultModel = null;
            try {
                roleService.delete(role.getId());
            } catch (SQLException e) {
                e.printStackTrace();
                resultModel = new ResultModel(500, "删除失败");
            }
            resultModel = new ResultModel(200, "删除成功");
            HttpServletResponse response = ServletActionContext.getResponse();
            ResponseUtils.writeJson(resultModel, response);
            return null;
        }

        public Integer getPage() {
            return page;
        }

        public void setPage(Integer page) {
            this.page = page;
        }

        public Integer getRows() {
            return rows;
        }

        public void setRows(Integer rows) {
            this.rows = rows;
        }

        public Role getRole() {
            return role;
        }

        public void setRole(Role role) {
            this.role = role;
        }
    }


